<?php
	class MsMovDAO{
		protected $db;

		public function __construct($db){
			$this->db = $db;
		}

		public function setDb($db){
			$this->db = $db;
		}

		public function getDb(){
			return $this->db;
		}
		
		public function getMsMov($id){
			$sql = "select MS_MOV, MS, MOV_MS, QTDE, TIPO, SALDO, SALDO_ATUAL, NF, PED, CLIEN, DT, DT_OCORRENCIA, OBS, LOCAL, EXPEDICAO
					from ms_mov					
					where ms_mov = {$id}";		
			$res = $this->getDb()->query($sql);
		
			if($res){			
				$r = $res[0];

				$cdao = new ClienDAO($this->getDb());
				$mvdao = new MovMsDAO($this->getDb());
				$msdao = new MsDAO($this->getDb());			
				$m = $mvdao->getMovMs($r['MOV_MS']);
				$ms = $msdao->getMs($r['MS']);
				
				$mm = new MsMov($r['MS_MOV'],$ms,$m,$r['QTDE'],$r['TIPO'],$c,$r['PED'],$r['NF'],$r['SALDO'],$r['SALDO_ATUAL'],$r['DT_OCORRENCIA'],$r['OBS'], $r['LOCAL'], $r['EXPEDICAO']);
				return $mm;			
			}
			else{
				return false;
			}
		}	

		public function getLista($where=array(),$order=array()){
			$lista = array();

			$where = ($where ? "where ".implode(" and ",$where) : "");
			$order = ($order ? "order by ".implode(", ",$order) : "");

			$sql = "select
						MS_MOV
					from ms_mov mv
					{$where}
					{$order}";
				
			$res = $this->getDb()->query($sql);				
			if($res){
				$mdao = new MsMovDAO($this->getDb());			
				foreach ($res as $r){
					$lista[] = $mdao->getMsMov($r['MS_MOV']);
				}			
			}					
			return $lista;
		}

		public function getSaldoMs($ms){
			$sql = "select MS_MOV, MS, MOV_MS, QTDE, TIPO, SALDO, SALDO_ATUAL, NF, PED, CLIEN, DT, DT_OCORRENCIA, OBS, LOCAL, EXPEDICAO
					from ms_mov					
					where ms = {$ms}
					and saldo_atual = 'S'";		
			$res = $this->getDb()->query($sql);
		
			if($res){			
				$r = $res[0];

				$cdao = new ClienDAO($this->getDb());
				$mvdao = new MovMsDAO($this->getDb());
				$msdao = new MsDAO($this->getDb());			
				$c = $cdao->getClien($r['CLIEN']);			
				$m = $mvdao->getMovMs($r['MOV_MS']);
				$ms = $msdao->getMs($r['MS']);
				
				$mm = new MsMov($r['MS_MOV'],$ms,$m,$r['QTDE'],$r['TIPO'],$c,$r['PED'],$r['NF'],$r['SALDO'],$r['SALDO_ATUAL'],$r['DT_OCORRENCIA'],$r['OBS'], $r['LOCAL'], $r['EXPEDICAO']);
				return $mm;			
			}	
		}		
	}
?>